<template>
  <el-dialog
    :title="!dataForm.keepId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="活动id" prop="actId">
      <el-input v-model="dataForm.actId" placeholder="活动id"></el-input>
    </el-form-item>
    <el-form-item label="商品id" prop="goodsId">
      <el-input v-model="dataForm.goodsId" placeholder="商品id"></el-input>
    </el-form-item>
    <el-form-item label="用户名" prop="userName">
      <el-input v-model="dataForm.userName" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="商品名" prop="goodsName">
      <el-input v-model="dataForm.goodsName" placeholder="商品名"></el-input>
    </el-form-item>
    <el-form-item label="商品规格" prop="goodsSize">
      <el-input v-model="dataForm.goodsSize" placeholder="商品规格"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="keepStart">
      <el-input v-model="dataForm.keepStart" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="订单状态" prop="keepStatus">
      <el-input v-model="dataForm.keepStatus" placeholder="订单状态"></el-input>
    </el-form-item>
    <el-form-item label="活动价格" prop="actMoney">
      <el-input v-model="dataForm.actMoney" placeholder="活动价格"></el-input>
    </el-form-item>
    <el-form-item label="最终价格" prop="keepMoney">
      <el-input v-model="dataForm.keepMoney" placeholder="最终价格"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          keepId: 0,
          userId: '',
          actId: '',
          goodsId: '',
          userName: '',
          goodsName: '',
          goodsSize: '',
          keepStart: '',
          keepStatus: '',
          actMoney: '',
          keepMoney: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          actId: [
            { required: true, message: '活动id不能为空', trigger: 'blur' }
          ],
          goodsId: [
            { required: true, message: '商品id不能为空', trigger: 'blur' }
          ],
          userName: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          goodsName: [
            { required: true, message: '商品名不能为空', trigger: 'blur' }
          ],
          goodsSize: [
            { required: true, message: '商品规格不能为空', trigger: 'blur' }
          ],
          keepStart: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          keepStatus: [
            { required: true, message: '订单状态不能为空', trigger: 'blur' }
          ],
          actMoney: [
            { required: true, message: '活动价格不能为空', trigger: 'blur' }
          ],
          keepMoney: [
            { required: true, message: '最终价格不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.keepId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.keepId) {
            this.$http({
              url: this.$http.adornUrl(`/kanjia/keep/info/${this.dataForm.keepId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.keep.userId
                this.dataForm.actId = data.keep.actId
                this.dataForm.goodsId = data.keep.goodsId
                this.dataForm.userName = data.keep.userName
                this.dataForm.goodsName = data.keep.goodsName
                this.dataForm.goodsSize = data.keep.goodsSize
                this.dataForm.keepStart = data.keep.keepStart
                this.dataForm.keepStatus = data.keep.keepStatus
                this.dataForm.actMoney = data.keep.actMoney
                this.dataForm.keepMoney = data.keep.keepMoney
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/kanjia/keep/${!this.dataForm.keepId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'keepId': this.dataForm.keepId || undefined,
                'userId': this.dataForm.userId,
                'actId': this.dataForm.actId,
                'goodsId': this.dataForm.goodsId,
                'userName': this.dataForm.userName,
                'goodsName': this.dataForm.goodsName,
                'goodsSize': this.dataForm.goodsSize,
                'keepStart': this.dataForm.keepStart,
                'keepStatus': this.dataForm.keepStatus,
                'actMoney': this.dataForm.actMoney,
                'keepMoney': this.dataForm.keepMoney
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
